package org.rc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.rc.entity.ConversationRecord;
import org.rc.mapper.ConversationRecordMapper;
import org.rc.service.ConversationRecordService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ConversationRecordServiceImpl extends ServiceImpl<ConversationRecordMapper, ConversationRecord> implements ConversationRecordService {

    @Override
    public void addRecord(ConversationRecord conversationRecord) {
        this.save(conversationRecord);
    }

    @Override
    public ConversationRecord getByConversationId(Long id) {
        return getById(id);
    }

    @Override
    public void end(ConversationRecord conversationRecord) {
        this.updateById(conversationRecord);
    }

    @Override
    public Page<ConversationRecord> getConversationRecordPage(long pageNo, long pageSize, Long roomId,Long userId,String beginTime,String endTime) {
        Page<ConversationRecord> page = new Page<>(pageNo,pageSize);
        QueryWrapper<ConversationRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNotNull("end_time");
        if(null !=roomId){
            queryWrapper.eq("room_id",roomId);
        }
        if(null !=userId){
            queryWrapper.eq("user_id",userId);
        }
        if(StringUtils.isNotEmpty(beginTime)){
            queryWrapper.ge("begin_time",beginTime);
        }
        if(StringUtils.isNotEmpty(endTime)){
            queryWrapper.le("end_time",endTime);
        }
        queryWrapper.orderByDesc("begin_time");
        return this.baseMapper.selectPage(page,queryWrapper);
    }

    @Override
    public void deleteByRecordId(Long id) {
        this.removeById(id);
    }

    @Override
    public void deleteByRecordIds(List<Long> ids) {
        this.removeByIds(ids);
    }
}
